package com.guigu.dh.service.impl;

import com.guigu.dh.cmn.feign.DictFeignClient;
import com.guigu.dh.common.result.Result;
import com.guigu.dh.pojo.cmn.Dict;
import com.guigu.dh.pojo.hosp.Department;
import com.guigu.dh.pojo.hosp.Hospital;
import com.guigu.dh.repository.DepartmentRepository;
import com.guigu.dh.repository.HospitalRepository;
import com.guigu.dh.service.HospitalService;
import com.guigu.dh.vo.hosp.DepartmentVO;
import com.guigu.dh.vo.hosp.HospitalQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;
    @Autowired
    private DepartmentRepository departmentRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    // 根据Id查询医院信息
    @Override
    public Result showDetails(String id) {
        Hospital hospital = hospitalRepository.findById(id).get();
        return Result.ok(hospital);
    }

    // 修改状态
    @Override
    public Result lockHospital(String id, String status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(Integer.parseInt(status));
        Hospital save = hospitalRepository.save(hospital);
        return Result.ok(save);
    }

    // 根据医院编号查医院信息
    @Override
    public Hospital getHospitalInfo(String hoscode) {
        Hospital hospital = new Hospital();
        hospital.setHoscode(hoscode);
        Example<Hospital> of = Example.of(hospital);
        Hospital optional = hospitalRepository.findOne(of).get();
        this.setHospitalParam(optional);
        return optional;
    }

    // 根据医院名称模糊查询医院集合
    @Override
    public Result findHospitalByHosname(String hosname) {
        List<Hospital> hospital = hospitalRepository.
                                    findHospitalByHosnameLike(hosname);

        return Result.ok(hospital);
    }


    // 分页查询的方法
    @Override
    public Result selectPage(Integer currentPage, Integer limit, HospitalQueryVO hospitalQueryVO) {
        // 分页条件
        PageRequest pageRequest = PageRequest.of(currentPage-1, limit);
        // 创建条件匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        Hospital h = new Hospital();
        if (hospitalQueryVO.getHoscode() != null && !"".equals(hospitalQueryVO.getHoscode())) {
            h.setHoscode(hospitalQueryVO.getHoscode());
        }
        if (hospitalQueryVO.getHosname() != null && !"".equals(hospitalQueryVO.getHosname())) {
            h.setHosname(hospitalQueryVO.getHosname());
        }
        if (hospitalQueryVO.getHostype() != null && !"-1".equals(hospitalQueryVO.getHostype())) {
            h.setHostype(hospitalQueryVO.getHostype());
        }
        if (hospitalQueryVO.getProvinceCode() != null && !"-1".equals(hospitalQueryVO.getProvinceCode())) {
            h.setProvinceCode(hospitalQueryVO.getProvinceCode());
            h.setDistrictCode(hospitalQueryVO.getProvinceCode());
            h.setCityCode(hospitalQueryVO.getProvinceCode());
        }

        Example<Hospital> of = Example.of(h, matcher);
        Page<Hospital> hospitals = hospitalRepository.findAll(of, pageRequest);
        /*Page<Hospital> hospitals = hospitalRepository.findAll(pageRequest);*/
        List<Hospital> content = hospitals.getContent();
        // 根据医院等级值查出等级名称
        // 调用方法封装
        content.stream().forEach(this::setHospitalParam);
        return Result.ok(hospitals);
    }

    private Hospital setHospitalParam(Hospital hospital){
        Result<Dict> byValue = dictFeignClient.findDictByValue("Hostype",hospital.getHostype());
        Map<String, Object> param = hospital.getParam();
        param.put("dictName",byValue.getData());
        return hospital;
    }
}
